﻿using UnityEngine;
using System.Collections;

public class SpriteManager : MonoBehaviour
{
    private UIAtlas m_Atlas = null;
    private GameObject m_CurrentAtlasObject = null;

    public string atlasPath { get; set; }
    public UIAtlas atlas 
    { 
        get 
        {
            if (m_Atlas == null)
            {
                m_Atlas = gameObject.GetComponent<UIAtlas>();
                if (m_Atlas == null) m_Atlas = gameObject.AddComponent<UIAtlas>();
            }
            return m_Atlas;
        } 
    }

    void Start()
    {
    }

    public bool LoadAtlas(string name)
    {
        GameObject newAtlasObject = Resources.Load(atlasPath + name) as GameObject;
        if (newAtlasObject == null)
        {
            Debug.LogError("Failed to load atlas, maybe, error in path or name?\n" + atlasPath + name);
            return false;
        }

        UIAtlas newAtlas = newAtlasObject.GetComponent<UIAtlas>();
        if (newAtlas == null)
        {
            Debug.LogError("Failed to ontain UIAtlas component from loaded object!");
            return false;
        }

        atlas.replacement = newAtlas;

        if (m_CurrentAtlasObject != null) Destroy(m_CurrentAtlasObject);
        m_CurrentAtlasObject = newAtlasObject;

        return true;
    }
}